﻿using FR.CodeVisualizer.ObjectSource;
using FR.CodeVisualizer.Views;
using FR.CodeVisualizer.Visualizers;
using Microsoft.VisualStudio.DebuggerVisualizers;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(CollectionVisualizer), typeof(CollectionVisualizerObjectSource), Target = typeof(List<>), Description = "FR Visualizers - Collections")]
[assembly: System.Diagnostics.DebuggerVisualizer(
    typeof(CollectionVisualizer), typeof(CollectionVisualizerObjectSource), Target = typeof(Dictionary<,>), Description = "FR Visualizers - Collections")]

namespace FR.CodeVisualizer.Visualizers
{
    public class CollectionVisualizer : DialogDebuggerVisualizer
    {
        public static void TestShowVisualizer(object objectToVisualize)
        {
            VisualizerDevelopmentHost visualizerHost = new VisualizerDevelopmentHost(objectToVisualize, typeof(CollectionVisualizer), typeof(CollectionVisualizerObjectSource), true);
            visualizerHost.ShowVisualizer();
        }
        protected override void Show(IDialogVisualizerService windowService, IVisualizerObjectProvider objectProvider)
        {
            if (windowService == null)
                throw new ArgumentNullException("windowService");
            if (objectProvider == null)
                throw new ArgumentNullException("objectProvider");

            //(objectProvider as ICustomVisualizerSource).Depth = RefectionOptionsContainer.Instance.Options.Depth;
            
            List<Entity> entities = objectProvider.GetObject() as List<Entity>;

            if (entities != null)
            {
                //inicializar o visualizador
                using (FormCollectionVisualizer main = new FormCollectionVisualizer())
                {
                    main.Init(entities);
                    //mostrar o visualizer
                    DialogResult dr = windowService.ShowDialog(main);

                    if (objectProvider.IsObjectReplaceable && dr == DialogResult.OK)
                        objectProvider.ReplaceObject(entities);
                }
            }
        }
    }

    public class RefectionOptions
    {
        public int Depth { get; set; }

        public string Ex { get; set; }
    }

    public class RefectionOptionsContainer
    {
        private static volatile RefectionOptionsContainer instance;
        private static object syncRoot = new Object();

        private RefectionOptionsContainer()
        {
            string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "CodeVisualizer");

            if (Directory.Exists(path) == false)
                Directory.CreateDirectory(path);

            fileName = Path.Combine(path, "CodeVisualizerOptions.xml");
        }

        public static RefectionOptionsContainer Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                            instance = new RefectionOptionsContainer();
                    }
                }

                return instance;
            }
        }

        string fileName;

        RefectionOptions options;
        public RefectionOptions Options
        {
            get
            {
                if (options == null)
                {
                    if (File.Exists(fileName))
                        options = FR.CodeVisualizer.Helpers.XmlSerializer.Deserialize<RefectionOptions>(fileName);
                    else
                        options = new RefectionOptions();
                }
                return options;
            }
            set
            {
                options = value;
                FR.CodeVisualizer.Helpers.XmlSerializer.Serializa(options, fileName);
            }
        }

        public void SaveOptions()
        {
            FR.CodeVisualizer.Helpers.XmlSerializer.Serializa(options, fileName);
        }
    }
}
